const net = require('net')
const readline = require('readline')

var port = 6527
var host = '127.0.0.1'
var socket = new net.Socket()
socket.setEncoding = 'UTF-8'

socket.connect(port, host, () => {
    socket.write('hello.')
})
socket.on('data', msg => {
    console.log(msg.toString());
    say()
})
socket.on('error', function (err) {
    console.log('error' + err);
})

socket.on('close', () => {

})
const r1 = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

function say() {
    r1.question('请输入：\n', (inputMsg) => {
        if (inputMsg != 'bye') {
            socket.write(inputMsg)
        } else {
            socket.destroy()
            r1.close()
        }
    })
}
// const client = net.createConnection({ port: 6527 }, () => {
//     console.log('通了');
//     client.write('world')
// })

// client.on('data', (data) => {
//     // client.end()
//     console.log(data.toString());
// })

// client.on('end', () => {
//     console.log('断开服务端');
// })


